/******************************************************************************/
#include "stdafx.h"
/******************************************************************************/

#include "cRechthoek.h"
#include "cLines.h"
#include "cEditor.h"

/******************************************************************************/
void InitPre() {
	App.name("music Scheme");
	App.flag=APP_NO_FX|APP_RESIZABLE|APP_FULL_TOGGLE|APP_MINIMIZABLE|APP_MAXIMIZABLE;
	IOPath("internals");
	Paks.add("internals/engine.pak");
}
/******************************************************************************/
Bool Init() {
	D.mode(1000, 800, 0);

	editor.create();
	background = WHITE;

	return true;
}
/******************************************************************************/
void Shut() {
}
/******************************************************************************/
Bool Update() {
	if(Kb.bp(KB_ESC))return false;
	Gui.update();

	// watch the color picker
	if (editor.colorPicker.hidden()) {
		editor.colorToGlobal = false;
	}

	if (editor.colorPicker.changed()) {
		if (editor.colorToGlobal) {
			editor.lines[editor.activeLine].color = editor.colorPicker();
			editor.globalColorButton->color = editor.colorPicker();
		}
	}

	return true;
}
/******************************************************************************/
void Draw() {
	D.clear(background);

	rh.setSteps(editor.division);
	lines.setSteps(editor.division);

	lines.offSet(editor.offset);
	if (editor.advancedBpm) {
		if (editor.bpmLogic.size()) {
			Int logicPos = 0;
			for (Int x = 0; x <= editor.steps; ) {
				lines.draw(x);
				x += editor.bpmLogic[logicPos];
				logicPos++;
				if (logicPos == editor.bpmLogic.size()) {
					logicPos = 0;
				}
			}
		}
	} else {
		for (Int x = 0; x <= editor.steps; x += editor.bpm) {
			lines.draw(x);
		}
	}
	
	// draw Voices
	for (Int x = 0; x < 10; x++) {
		rh.setLine(x + 2);
		rh.drawCaption(editor.lines[x].caption);
		
		for (Int y = 0; y < editor.lines[x].blocks.size(); y++) {
			// set color
			if (editor.lines[x].blocks[y].color != BLACK) {
				rh.setColor(editor.lines[x].blocks[y].color);
			} else {
				rh.setColor(editor.lines[x].color);
			}
			// add caption if needed
			if (editor.lines[x].blocks[y].caption.length()) {
				rh.draw(editor.lines[x].blocks[y].begin, editor.lines[x].blocks[y].end, editor.lines[x].blocks[y].caption);
			} else {
				rh.draw(editor.lines[x].blocks[y].begin, editor.lines[x].blocks[y].end);
			}
		}
	}

	if (editor.makeScreenshot) {
		Image dump, cropped;
		Renderer.capture(dump);
		dump.crop(cropped, 0, 0, D.x(), (D.y()/2));
		cropped.ExportPNG(editor.imageName);
		editor.makeScreenshot = false;
	}

	Gui.draw();
}
/******************************************************************************/
